package com.dianxin.smartpark.service.property.impl;

import java.util.List;

import com.chinatelecom.response.common.CommonResponseBody;
import com.chinatelecom.response.utils.BeanUtil;
import com.chinatelecom.response.utils.ResponseBodyWrapperUtil;
import com.chinatelecom.response.constant.ErrMsgEnum;

import com.chinatelecom.response.utils.StringUtils;
import com.dianxin.smartpark.dao.dataobject.property.SmartParkPropertyWater;
import com.dianxin.smartpark.dao.dataobject.property.SmartParkPropertyWaterExample;
import com.dianxin.smartpark.dao.mapper.property.SmartParkPropertyWaterMapper;
import com.dianxin.smartpark.dao.qo.property.SmartParkPropertyWaterQO;
import com.dianxin.smartpark.manager.order.ISmartParkOrderRecordService;
import com.dianxin.smartpark.service.property.SmartParkPropertyWaterService;
import com.dianxin.smartpark.service.utils.OrderNoUtil;
import com.dianxin.smartpark.service.vo.property.SmartParkPropertyWaterVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 送水管理业务层处理
 * 
 * @author chenjiasong
 * @date 2021-07-22
 */
@Service
public class SmartParkPropertyWaterServiceImpl implements SmartParkPropertyWaterService
{
    private final static Logger logger = LoggerFactory.getLogger(SmartParkPropertyWaterServiceImpl.class);
    @Resource
    private SmartParkPropertyWaterMapper smartParkPropertyWaterMapper;

    @Resource
    private ISmartParkOrderRecordService iSmartParkOrderRecordService;

    /**
     * 查询送水管理
     *
     * @param qo 送水管理ID
     * @return 送水管理
     */
    @Override
    public CommonResponseBody<SmartParkPropertyWaterVO> selectSmartParkPropertyWater(SmartParkPropertyWaterQO qo)
    {
        if(StringUtils.isNull(qo.getId())){
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
        }
        SmartParkPropertyWaterExample smartParkPropertyWaterExample = new SmartParkPropertyWaterExample();
        SmartParkPropertyWaterExample.Criteria criteria = smartParkPropertyWaterExample.createCriteria();
        if (qo.getTenantId() != null) {
            criteria.andTenantIdEqualTo(qo.getTenantId());
        }
        criteria.andIsDeletedEqualTo(0);
        criteria.andIdEqualTo(qo.getId());
        List<SmartParkPropertyWater> passRecords = smartParkPropertyWaterMapper.selectByExample(smartParkPropertyWaterExample);
        return ResponseBodyWrapperUtil.successWrapper(StringUtils.isNotEmpty(passRecords)?passRecords.get(0):null,SmartParkPropertyWaterVO.class);
    }

    /**
     * 查询送水管理列表
     * 
     * @param qo 送水管理
     * @return 送水管理
     */
    @Override
    public CommonResponseBody<List<SmartParkPropertyWaterVO>> selectSmartParkPropertyWaterList(SmartParkPropertyWaterQO qo)
    {
        SmartParkPropertyWaterExample smartParkPropertyWaterExample = new SmartParkPropertyWaterExample();
        SmartParkPropertyWaterExample.Criteria criteria = smartParkPropertyWaterExample.createCriteria();
        if (qo.getTenantId() != null) {
            criteria.andTenantIdEqualTo(qo.getTenantId());
        }
        criteria.andIsDeletedEqualTo(0);
        if (qo.getNeedPage()) {
            smartParkPropertyWaterExample.setOrderByClause("gmt_create desc limit " + (qo.getPageIndex() - 1) * qo.getPageSize() + "," + qo.getPageSize());
        } else {
            smartParkPropertyWaterExample.setOrderByClause("gmt_create desc");
        }
        List<SmartParkPropertyWater> passRecords = smartParkPropertyWaterMapper.selectByExample(smartParkPropertyWaterExample);
        long count = smartParkPropertyWaterMapper.countByExample(smartParkPropertyWaterExample);
        return ResponseBodyWrapperUtil
                .successListWrapper(passRecords, count, qo, SmartParkPropertyWaterVO.class);
    }

    /**
     * 新增送水管理
     * 
     * @param vo 送水管理
     * @return 结果
     */
    @Override
    public CommonResponseBody<Boolean> insertSmartParkPropertyWater(SmartParkPropertyWaterVO vo)
    {
        // 验证 todo
        SmartParkPropertyWater smartParkPropertyWater = new SmartParkPropertyWater();
        try {
            BeanUtil.insertConversion(vo, smartParkPropertyWater);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkPropertyWater.beforeInsert();
        smartParkPropertyWater.setStatus(0);
        String orderNo = OrderNoUtil.getOrderNo("SS");
        smartParkPropertyWater.setOrderNo(orderNo);
        smartParkPropertyWater.beforeInsert();
        smartParkPropertyWater.setCreator("1435058739601235968");
        int res = smartParkPropertyWaterMapper.insertSelective(smartParkPropertyWater);
        if(res > 0){
            res = iSmartParkOrderRecordService.insertSmartParkOrderRecord(vo.getTenantId(),0,vo.getCreator(),smartParkPropertyWater.getId(),3,1,"提交","提交");
        }
        return ResponseBodyWrapperUtil.toResData(res);
    }

    /**
     * 修改送水管理
     * 
     * @param vo 送水管理
     * @return 结果
     */
    @Override
    public CommonResponseBody<Boolean> updateSmartParkPropertyWater(SmartParkPropertyWaterVO vo)
    {
        // 验证 todo
        SmartParkPropertyWater smartParkPropertyWater = new SmartParkPropertyWater();
        try {
            BeanUtil.insertConversion(vo, smartParkPropertyWater);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkPropertyWater.beforeUpdate();
        return ResponseBodyWrapperUtil.toResData(smartParkPropertyWaterMapper.updateByPrimaryKeySelective(smartParkPropertyWater));
    }

    /**
     * 删除送水管理信息
     *
     * @param vo 送水管理
     * @return 结果08
     */
    @Override
    public CommonResponseBody<Boolean> deleteSmartParkPropertyWater(SmartParkPropertyWaterVO vo)
    {
        // 验证 todo
        if(StringUtils.isNull(vo.getId())){
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
        }
        SmartParkPropertyWater smartParkPropertyWater = new SmartParkPropertyWater();
        try {
            BeanUtil.insertConversion(vo, smartParkPropertyWater);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkPropertyWater.beforeDelete();
        return ResponseBodyWrapperUtil.toResData(smartParkPropertyWaterMapper.updateByPrimaryKeySelective(smartParkPropertyWater));
    }
}
